package com.yxmadoc.umlproject.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ResponseBody
	@ExceptionHandler(Exception.class)
	public Object handleException(Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));  // 记录错误信息
		String msg = e.getMessage();
		if (msg == null || msg.equals("")) {
			msg = "错误不详，请查看调用堆栈.";
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("message", msg);
		return jsonObject;
	}
}
